<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 星云微讯科技有限公司
 * 手机端页面
 *  wap/ajax.php
 *  lichaoliang  2015-04-10
 */

class Ajax extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('comm_model');
        $this->load->model('hot_model');
        $this->load->model('recommend_model');
        $this->load->model('prize_model');
        header("Content-type:text/html;charset=utf-8");
    }

    /**
     *  ajax 分页  最新游戏
    */
    public function prize(){

        $data = array();
        $page = !empty($_GET['ajaxPage'])?intval($_GET['ajaxPage']):3;
        $view['title'] = !empty($_GET['title'])?addslashes($_GET['title']):'';
        $per_page = 5;
        $page=($page-1) * $per_page;

        if($page>50){//只显示最新的50个游戏
            $data['msg']=$page;
            echo json_encode($data);exit;
        }

        $arr = $this->hot_model->get_list($page,$per_page,$view,true);
        foreach($arr as $key=>$val){
            $arr[$key]['picture'] = base_url().$val['picture'];
        }

        $data['msg']=1;
        $data['arrs']=$arr;
        $data['length']=count($arr);
        echo json_encode($data);

        $this->_save_search_log($view['title'], $data['length']);
    }

    /**
     *  ajax 分页  热门游戏
     */
    public function hot(){
        $page = !empty($_GET['ajaxPage'])?intval($_GET['ajaxPage']):3;
        $view['title'] = !empty($_GET['title'])?addslashes($_GET['title']):'';
        $per_page = 5;
        $page=($page-1) * $per_page;
        $arr = $this->hot_model->get_list($page,$per_page,$view);
        foreach($arr as $key=>$val){
            $arr[$key]['picture'] = base_url().$val['picture'];
        }
        $data = array();

        $data['msg']=1;
        $data['arrs']=$arr;
        $data['length']=count($arr);
        echo json_encode($data);

        $this->_save_search_log($view['title'], $data['length']);
    }

    /**
     *  ajax 分页  热门游戏
     */
    public function recommend(){
        $this->load->library('mobile_detect');  // 判断手机操作系统
        $phone = 1;
        if($this->mobile_detect->isWindowsMobileOS()){
            $phone = 3;
        }elseif($this->mobile_detect->isWindowsPhoneOS()){
            $phone = 3;
        }elseif($this->mobile_detect->isAndroidOS()){
            $phone = 1;
        }elseif($this->mobile_detect->isiPhone()){
            $phone = 2;
        }elseif($this->mobile_detect->isiPad()){
            $phone = 2;
        }

        $page = !empty($_GET['ajaxPage'])?intval($_GET['ajaxPage']):2;
        $view['breakid'] = !empty($_GET['remmendType'])?intval($_GET['remmendType']):1;
        $view['title'] = !empty($_GET['title'])?addslashes($_GET['title']):'';
        $view['type'] = $phone;
        $per_page = 5;
        $page=($page-1) * $per_page;
        $arr = $this->recommend_model->get_list($page,$per_page,$view);
        foreach($arr as $key=>$val){
            $arr[$key]['picture'] = base_url().$val['picture'];
        }
        $data = array();

        $data['msg']=1;
        $data['arrs']=$arr;
        $data['length']=count($arr);
        echo json_encode($data);
    }

    /**
     * 游戏历史记录
     */
    public function history(){
        $this->load->model('visitlog_model');
        $this->load->model('user_model');
        $user_openid = $this->user_model->get_user_info();
        $data = $this->visitlog_model->get_history($user_openid);
        echo json_encode($data);
    }

    /**
     * 保存搜索记录
     * @param $search_content
     * @param $result_count
     */
    private function _save_search_log($search_content, $result_count){
        if(!empty($search_content)){
            $this->load->model('searchlog_model');
            $this->load->model('user_model');
            $user_openid = $this->user_model->get_user_info();
            $this->searchlog_model->add_search_log($user_openid, $search_content, $result_count);
        }
    }
}